
package org.openeai.lite.console.beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.openeai.lite.common.control.ESBController;
import org.openeai.lite.common.exception.OpenEAIException;
import org.openeai.lite.common.util.Constants;
import org.openeai.lite.common.util.DateTimeUtils;
import org.openeai.lite.esb.components.StatsComponent;


@ManagedBean(name = "esbBean")
@SessionScoped
public class ESBBean implements Serializable
{

    private static final long serialVersionUID = 1L;

    // Properties
    private boolean started = false;
    
    private String startupType;

    private String status;

    private String mulestats;

    private Integer flows;

    private String muleid;

    private String startdate;

    private String domainid;

    private boolean newFlowView = false;

    public boolean isStarted()
    {
        started = ESBController.getInstance().isStarted();
        return started;
    }

    public String getStartupType()
    {
        try
        {
            this.startupType = ESBController.getInstance().getStartupType();
        }
        catch (Exception e)
        {
            this.startupType = Constants.UNAVAILABLE;
        }

        return startupType;
    }

    // Mule status, active or inactive
    public String getStatus()
    {
        if (ESBController.getInstance().isStarted())
        {
            this.status = Constants.ACTIVE;
        }
        else
        {
            this.status = Constants.INACTIVE;
        }
        return status;
    }


    // Number of deployed message flows
    public Integer getflows() throws OpenEAIException
    {
        try
        {
            flows = ESBController.getInstance().getContext().getStatistics().getServiceStatistics()
                            .size();
        }
        catch (Exception e)
        {
            // Mule probably down
            flows = 0;
        }
        return flows;
    }


    // List of Mule statistics, presented as single String
    public String getMulestats() throws OpenEAIException
    {
        try
        {
            this.mulestats = new StatsComponent().getMuleStats(ESBController.getInstance()
                            .getContext());
        }
        catch (Exception e)
        {
            // Mule probably down
            this.mulestats = Constants.UNAVAILABLE;
        }

        return this.mulestats;
    }


    // Mule ID unique number
    public String getMuleid()
    {
        try
        {
            this.muleid = ESBController.getInstance().getContext().getConfiguration().getId();
        }
        catch (Exception e)
        {
            // Mule probably down
            this.muleid = Constants.UNKNOWN;
        }
        return this.muleid;
    }


    // Mule startup date/time, nicely formatted
    public String getStartdate()
    {
        try
        {
            Long date = ESBController.getInstance().getContext().getStartDate();
            this.startdate = new DateTimeUtils().millisToFormattedDate(date);
        }
        catch (Exception e)
        {
            // Mule probably down
            this.startdate = Constants.UNKNOWN;
        }
        return startdate;
    }


    // Host on which Mule runs
    public String getDomainid()
    {
        try
        {
            domainid = ESBController.getInstance().getContext().getConfiguration().getDomainId();
        }
        catch (Exception e)
        {
            // Mule probably down
            domainid = Constants.UNKNOWN;
        }
        return domainid;
    }


    // status for rendering New Flow Registration Form
    public boolean getNewFlowView()
    {
        return this.newFlowView;
    }

    public void setNewFlowView(boolean newFlowView)
    {
        this.newFlowView = newFlowView;
    }

    public void toggleNewFlowView()
    {
        this.newFlowView = !this.newFlowView;
    }

}
